<template>
    <!-- template 元素下面只能存在一个标签 -->
    <div>
        <el-container>
            <el-header>Header</el-header>
            <el-main>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="username" class="pr-item">
                        <el-input v-model.number="ruleForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password" class="pr-item">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
</template>

<script>
    export default {
        data() {
            var checkUsername = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('用户名不能为空'));
                } else {
                    return callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    password: '123456',
                    username: 'pr'
                },
                rules: {
                    password: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    username: [
                        { validator: checkUsername, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //在这里的this是指整个vue工程的对象
                        // 当进入到axios请求中去后 在请求中的this代表的这个请求的this
                        //因此在这里预存以下vue 中的this
                        const _this = this;
                        this.$axios.post("/mUser/login", this.ruleForm).then(res =>{
                            const userinfo = res.data.data
                            const jwt = res.headers.authorization;
                            //store.commit() 方法会调用到 store 中mutations中的方法
                            _this.$store.commit("SET_TOKEN", jwt);
                            _this.$store.commit("SET_USERINFO", userinfo)

                            //跳转详情页面
                            _this.$router.push("/")
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    .el-header, .el-footer {
        background-color: #42b983;
        color: #333;
        text-align: center;
        line-height: 60px;
    }

    .el-aside {
        background-color: #FFFFFF;
        color: #333;
        text-align: center;
        line-height: 200px;
    }

    .el-main {
        background-color: #FFFFFF;
        color: #333;
        text-align: center;
        line-height: 160px;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }
    .pr-item {
        width: 30%;
        margin: 0 auto;
    }
</style>